﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;

namespace Nirvana
{
    [CustomEditor(typeof(ProductNameI18N))]
    public class ProductNameI18NEditor : Editor
    {
        private SerializedProperty productNames;
        private ReorderableList reorderableList;
        /// <summary>
        /// 记录创建的language下标
        /// </summary>
        private HashSet<int> languageIndexHash = new HashSet<int>();
        private Dictionary<int, int> languageMap = new Dictionary<int, int>();

        public override void OnInspectorGUI()
        {
            base.serializedObject.Update();
            this.reorderableList.DoLayoutList();
            if (base.serializedObject.ApplyModifiedProperties())
            {
                this.RefreshLanguageState();
            }
            ProductNameI18N productNameI18N = (ProductNameI18N)this.target;
            EditorGUILayout.Space(10f);
            EditorGUILayout.HelpBox("Android在res目录下创建国际化AppName,IOS需要先Active，再Build才能创建", MessageType.Info);
            if (GUILayoutEx.ActionButton("Active Setting"))
            {
                productNameI18N.Active();
            }
        }

        private void OnEnable()
        {
            if (this.target == null) return;
            this.productNames = serializedObject.FindProperty("productNames");
            this.reorderableList = new ReorderableList(serializedObject, this.productNames);
            this.reorderableList.drawHeaderCallback = OnDrawHead;
            this.reorderableList.elementHeight =2f* EditorGUIUtility.singleLineHeight;
            this.reorderableList.drawElementCallback = OnDrawElement;
            RefreshLanguageState();
        }

        protected void OnDrawHead(Rect rect)
        {
            GUI.Label(rect, "App Names:");
        }

        protected void OnDrawElement(Rect rect, int index, bool isActive, bool isForce)
        {
            SerializedProperty arrayElementAtIndex = this.productNames.GetArrayElementAtIndex(index);
            bool flag = this.languageIndexHash.Contains(index);
            Color color = GUI.color;
            if (flag)
            {
                GUI.color = new Color(1f, 0.5f, 0.5f, 1f);
            }
            SerializedProperty Language = arrayElementAtIndex.FindPropertyRelative("Language");
            SerializedProperty AppName = arrayElementAtIndex.FindPropertyRelative("AppName");
            Rect r0 = new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight);
            EditorGUI.PropertyField(r0, Language);
            r0.y += EditorGUIUtility.singleLineHeight;
            EditorGUI.PropertyField(r0, AppName);
            GUI.color = color;
        }
        private void RefreshLanguageState()
        {
            this.languageIndexHash.Clear();
            this.languageMap.Clear();
            for (int i = 0; i < this.productNames.arraySize; i++)
            {
                SerializedProperty arrayElementAtIndex = this.productNames.GetArrayElementAtIndex(i);
                SerializedProperty Language = arrayElementAtIndex.FindPropertyRelative("Language");
                if (this.languageMap.ContainsKey(Language.intValue))
                {
                    this.languageIndexHash.Add(this.languageMap[Language.intValue]);
                    this.languageIndexHash.Add(i);
                }
                else
                {
                    this.languageMap.Add(Language.intValue, i);
                }
            }
        }

    }
}

